package com.example.demo.Controller;

import com.example.demo.Bean.Product;
import com.example.demo.Service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
public class FileController {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ProductService productService;

    private Integer idByProduct;
    private String Category;

    public Integer getIdByProduct() {
        return idByProduct;
    }

    public void setIdByProduct(Integer idByProduct) {
        this.idByProduct = idByProduct;
    }

    public String getCategory() {
        return Category;
    }

    public void setCategory(String category) {
        Category = category;
    }

    @PostMapping("/upproduct")
    public Boolean upproduct(String shop,String brand,String category,String name,Double price,Integer stock){
        try{
            Product product = new Product();
            product.setCategory(category);
            product.setTitle(name);
            product.setPrice(price);
            product.setShop(shop);
            product.setStock(stock);
            product.setBrand(brand);
            setCategory(category);
            /*
            上传商品出问题,这个try没有执行完成,上面的参数是传进来了,应该是数据库操作出问题
             */
            productService.insertProduct(product);
            int idByProduct = productService.getIdByProduct(product);
            setIdByProduct(idByProduct);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @RequestMapping("/elupload")
    public String elUpload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        String filePath = "D:/warren_market/src/assets/" + getCategory() + "/" + getIdByProduct() +"/" + fileName;
        makeDir(filePath);
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
            String path = "D:/warren_market/src/assets/" + getCategory() + "/" + getIdByProduct() +"/";
            File files = new File(path);
            File[] fs = files.listFiles();
            Integer nums = 1;
            for (File f1 : fs) {
                String str = nums.toString() + ".jpg";
                f1.renameTo(new File(path + "/" + str));     //下面的这些就不用管了,需要注意的是此行有个path如果上面改了这是唯一需要修改的地方
                nums++;
            }
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        return "False";
    }

    private static void makeDir(String filePath) {
        if (filePath.lastIndexOf('/') > 0) {
            String dirPath = filePath.substring(0, filePath.lastIndexOf('/'));
            File dir = new File(dirPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
        }
    }

}
